//This is the class for the board.
//More functions may be added if needed, of course.
import java.util.Random;

public class Board{
	public enum Square {water, ship, hit, miss}
	private Square[][] grid;
	private final int SIZE = 10;
	private Random rand = new Random();
	
	public Board(boolean isComputer) {
		grid = new Square[SIZE][SIZE];
		for (int i=0; i<SIZE; i++) {	//Sets all the Squares in the array as water
			for (int j=0; j<SIZE; j++) {
				grid[i][j] = Square.water;
			}
		}
		if(isComputer) {
			System.out.println("Computer: ");
		}
		else {
			System.out.println("Player: ");
		}
		System.out.println(" ------------------- ");
		for(int i=0; i<10; i++) {
			System.out.print('|');
			for(int j=0; j<10; j++) {
				System.out.print(grid[i][j].ordinal() + " ");
			}
			System.out.println('|');
		}
		System.out.println(" ------------------- ");
		System.out.println();
	}
	
	public boolean setShip (int startRow, int startCol, int direction /*1 is horizontal, 2 is vertical*/, int shipLength) {
		//This function will set squares starting at Square[startRow][startCol] going shipLength in d as Square.ship
		boolean isSet = false, intersect = false;
		try {
			if (direction == 1 && (startCol + (shipLength-1)) < 10) {
				for(int i=startCol; i<shipLength + startCol; i++) {
					if(grid[startRow][i] != Square.water) {
						intersect = true;
						isSet = false;
					}
				}
				if(!intersect) {
					for(int i=startCol; i<shipLength + startCol; i++) {
						grid[startRow][i] = Square.ship;
					}
					isSet = true;
				}
			}
			else if (direction == 2 && (startRow + (shipLength-1)) < 10) {
				for(int i=startRow; i<shipLength + startRow; i++) {
					if(grid[i][startCol] != Square.water) {
						intersect = true;
						isSet = false;
					}
				}
				if(!intersect) {
					for(int i=startRow; i<shipLength + startRow; i++) {
						grid[i][startCol] = Square.ship;
					}
					isSet = true; 
				}
			}
		}
		catch (ArrayIndexOutOfBoundsException a) {}
		System.out.println(" ------------------- ");
		for(int i=0; i<10; i++) {
			System.out.print('|');
			for(int j=0; j<10; j++) {
				System.out.print(grid[i][j].ordinal() + " ");
			}
			System.out.println('|');
		}
		System.out.println(" ------------------- ");
		System.out.println();
		return isSet;
	}
	
	public int shootSquare (int x, int y) {
		//This function will check if the Square at the given (x, y) is a new hit, new miss, or has already been fired on (represented by returning Square.ship)
		if (grid[x][y] == Square.ship) {
			grid[x][y] = Square.hit;
				System.out.println(" ------------------- ");
		for(int i=0; i<10; i++) {
			System.out.print('|');
			for(int j=0; j<10; j++) {
				System.out.print(grid[i][j].ordinal() + " ");
			}
			System.out.println('|');
		}
		System.out.println(" ------------------- ");
		System.out.println();
			return grid[x][y].ordinal();
		}
		else if (grid[x][y] == Square.water) {
			grid[x][y] = Square.miss;
				System.out.println(" ------------------- ");
		for(int i=0; i<10; i++) {
			System.out.print('|');
			for(int j=0; j<10; j++) {
				System.out.print(grid[i][j].ordinal() + " ");
			}
			System.out.println('|');
		}
		System.out.println(" ------------------- ");
		System.out.println();
			return grid[x][y].ordinal();
		}
		else {
			System.out.println(" ------------------- ");
		for(int i=0; i<10; i++) {
			System.out.print('|');
			for(int j=0; j<10; j++) {
				System.out.print(grid[i][j].ordinal() + " ");
			}
			System.out.println('|');
		}
		System.out.println(" ------------------- ");
		System.out.println();
			return Square.ship.ordinal();
		}
		
	}
	
	public int checkSquare (int x, int y) {
		return grid[x][y].ordinal();
	}
	//cherries
	public int[] randomSetShip(int shipsize){
		int aistartcol;
		int aistartrow;
		int dir;
		do{
			aistartcol = rand.nextInt(10);
			aistartrow = rand.nextInt(10);
			dir = rand.nextInt(2);
		}while (!setShip(aistartrow, aistartcol, dir + 1, shipsize));
		int[] shipinfo = new int[3];
		shipinfo[0] = aistartcol;
		shipinfo[1] = aistartrow;
		shipinfo[2] = dir;
		return shipinfo;
	}
}